<template>
	<view class="container">
		<!-- <view class="topbox">
			<u-button type='primary' @click="clearClick">清理</u-button>
		</view> -->

		<block v-if="notifications.length == 0">
			<view class="shoproempty">
				<shopro-empty :image="mediaUrl + '/media/image/empty_goods.png'" tipText="暂无~"></shopro-empty>
			</view>
		</block>
		<block v-else>
			<u-list>
				<u-list-item v-for="(item, index) in notifications" :key="index">
					<view class="time">{{item.date}}</view>
					<view class="box" :id="item.unread ? 'wei' : ''">
						<view class="left">
							<u-avatar shape="square" :src="mediaUrl+'/media/avatar/asd.png'"></u-avatar>
						</view>
						<view class="des">
							{{item.verb}}
						</view>
					</view>
				</u-list-item>
			</u-list>
		</block>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				mediaUrl: uni.$http.mediaUrl,
				userInfo: null,
				show: false,
				notifications: [],
				text: '无头像',
			};
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
			this.getNotifications()
			console.log(this.userInfo)
		},
		onPullDownRefresh(e) {
			console.log("上拉")

			this.getNotifications();
		},
		onUnload: function onUnload() {
			if (this.notifications.length > 0) {
				this.clearClick()
			}

		},
		methods: {
			async getNotifications() {
				const postmsg = {
					username: this.userInfo.username,
				}
				const {
					data: res
				} = await uni.$http.post('getNotifications/', postmsg)
				if (res.code == 200) {
					uni.stopPullDownRefresh(); //上拉停止刷新
					// if (res.data.notification.length>0){

					// 	for (let i = 0; i < res.data.notification.length; i++) {
					// 		if(res.data.notification[i].verb.indexOf("\\r") >= 0){
					// 			console.log(res.data.notification[i].verb)
					// 		}
					// 		// res.data.notification[i].verb = res.data.notification[i].verb.replace(/\n/g, '')

					// 	}
					// }
					this.notifications = []
					this.notifications = res.data.notification
					console.log('获取消息:', this.notifications);
				} else {
					console.log('获取消息失败', res.msg);
				}
			},
			async clearClick() {
				const postmsg = {
					username: this.userInfo.username
				}
				const {
					data: res
				} = await uni.$http.post('clearNotifications/', postmsg)
				if (res.code == 200) {
					uni.showToast({
						icon: 'success',
						title: '全部已读！',
						duration: 1000
					})
					setTimeout(() => {
						this.getNotifications()
						// uni.reLaunch({
						// 	url: '/pages/conversations/conversations'
						// })
					}, 1000);

				} else {
					console.log('获取消息失败', res.msg);
				}
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f2f2f2;
		height: 100%;
	}

	.topbox {
		width: 80%;
		margin: 10px 10%;
	}

	.box {
		font-size: 28rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
		width: 72vw;
		align-items: flex-start;
		display: flex;
		align-items: center;

		//border-bottom: 2rpx solid #eee;
		.left {
			flex-shrink: 0;
			margin-right: 20rpx;
		}

		.des {
			color: #543d43;
			word-break: break-all;
			word-wrap: break-word;
			white-space: pre-line;
			width: 100%;
			// display: flex;
			background-color: #fff;
			border-radius: 30rpx;
			padding: 20rpx;
			flex: 1;
		}
	}

	.time {
		font-size: 24rpx;
		color: #c1c1c1;
		padding-top: 20rpx;

		display: flex;
		align-items: center;
		justify-content: center;
	}

	#wei {
		.time {
			position: relative;

			&:after {
				position: absolute;
				right: 0;
				top: 0;
				content: '未读';
				display: inline-block;
				color: #d81e06;
				font-size: 24rpx;
			}
		}
	}

	.u-avatar__image--square {
		border-radius: 50% !important;

	}
</style>